package com.burst.reptile.modular.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.burst.reptile.common.constant.UserConstants;
import com.burst.reptile.common.utils.poi.ExcelUtil;
import com.burst.reptile.framework.aspectj.lang.annotation.Log;
import com.burst.reptile.framework.aspectj.lang.enums.BusinessType;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.LayuiResult;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.system.entity.SysDictType;
import com.burst.reptile.modular.system.service.ISysDictTypeService;

import lombok.RequiredArgsConstructor;


/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author wujin
 * @since 2020-05-22
 */
@RestController
@RequestMapping("/system/dict/type")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysDictTypeController extends BaseController {
	
	private final ISysDictTypeService dictTypeService;

	/**
	 * 获取字典类型列表
	 * @param dictType
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('system:dict:list')")
	@GetMapping("/list")
	public LayuiResult<Object> list(SysDictType dictType) {
		Page<SysDictType> page = startPage();
		Page<SysDictType> list = dictTypeService.selectPageDictTypeList(page, dictType);
		return LayuiResult.success(list.getTotal(), list.getRecords());
	}

	/**
	 * 导出字典类型
	 * @param dictType
	 * @return
	 */
	@Log(title = "字典类型", businessType = BusinessType.EXPORT)
	@PreAuthorize("@ss.hasPermi('system:dict:export')")
	@GetMapping("/export")
	public Result<Object> export(SysDictType dictType) {
		List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
		ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
		return util.exportExcel(list, "字典类型");
	}

	/**
	 * 新增字典类型
	 */
	@PreAuthorize("@ss.hasPermi('system:dict:add')")
	@Log(title = "字典类型", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	public Result<Object> add(@Validated @RequestBody SysDictType dict) {
		if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
			return Result.error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
		}
		return Result.success(dictTypeService.insertDictType(dict));
	}

	/**
	 * 修改字典类型
	 */
	@PreAuthorize("@ss.hasPermi('system:dict:edit')")
	@Log(title = "字典类型", businessType = BusinessType.UPDATE)
	@PutMapping("/edit")
	public Result<Object> edit(@Validated @RequestBody SysDictType dict) {
		if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
			return Result.error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
		}
		return Result.success(dictTypeService.updateDictType(dict));
	}

	/**
	 * 删除字典类型
	 */
	@PreAuthorize("@ss.hasPermi('system:dict:remove')")
	@Log(title = "字典类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove/{dictIds}")
	public Result<Object> remove(@PathVariable Long[] dictIds) {
		return Result.success(dictTypeService.deleteDictTypeByIds(dictIds));
	}

}
